﻿using System;
using System.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Evpo.ContactData.Data
{
    internal abstract class StatementExecutionBase : IDisposable
    {
        internal StatementExecutionBase(Connection conn, string procedureName) :
            this(conn, procedureName, Enumerable.Empty<SqlParameter>())
        {
        }

        internal StatementExecutionBase(Connection conn, string procedureName, IEnumerable<SqlParameter> parameters)
        {
            Connection = conn;
            Command = conn.SqlConn.CreateCommand();
            Command.CommandText = procedureName;
            Command.CommandType = System.Data.CommandType.StoredProcedure;
            Command.Parameters.AddRange(parameters.ToArray());
        }


        #region Fields
        #endregion

        #region Properties
        internal Connection Connection
        {
            get;
            private set;
        }

        internal SqlCommand Command
        {
            get;
            private set;
        }

        #endregion

        #region Methods
        #endregion Methods

        #region IDisposable
        public virtual void Dispose()
        {
            if (Connection.IsOpen)
                Connection.Close();
        }
        #endregion

    }
}
